/*
 * Copyright (C) 2009 M. Hautle.
 * 
 * This file is part of TrackedBean.
 * 
 * TrackedBean is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * TrackedBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with TrackedBean. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.trackedbean.copier.internal;

import ch.trackedbean.copier.*;
import ch.trackedbean.copier.annotations.*;
import ch.trackedbean.loaders.lazy.*;

/**
 * Some default {@link IMappingFilter}s.
 * 
 * @author M. Hautle
 */
public enum DefaultMappingTypes implements IMappingFilter {
    /** Copy all attributes excepting those annotated with {@link LoadLazy}. */
    NO_LAZY {
        @Override
        public boolean check(AbstractSimpleMappingHolder h) {
            return !h.isLazy();
        }
    },
    /** Copy all attributes excepting those annotated with {@link DeepMapping}. */
    NO_DEEP {
        @Override
        public boolean check(AbstractSimpleMappingHolder h) {
            return !(h instanceof DeepMappingHolder);
        }
    },
    /** Copy all attributes excepting those annotated with {@link DeepMapping} or {@link LoadLazy}. */
    FLAT {
        @Override
        public boolean check(AbstractSimpleMappingHolder h) {
            return !(h.isLazy() || h instanceof DeepMappingHolder);
        }
    },
    /** Copy all attributes. */
    ALL {
        @Override
        public boolean check(AbstractSimpleMappingHolder h) {
            return true;
        }
    };

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean copyDst2Src(IMappingHolder holder) {
        if (holder instanceof AbstractSimpleMappingHolder)
            return check((AbstractSimpleMappingHolder) holder);
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean copySrc2Dst(IMappingHolder holder) {
        if (holder instanceof AbstractSimpleMappingHolder)
            return check((AbstractSimpleMappingHolder) holder);
        return true;
    }

    /**
     * Checks if the property should be copied.
     * 
     * @param holder The mapping to check
     * @return True if the value should be copied
     */
    abstract boolean check(AbstractSimpleMappingHolder holder);

    /**
     * Creates a filer which accepts only {@link IMappingHolder}s which have the same profile set in {@link IMappingHolder#getProfile()}.
     * 
     * @param profile The profile name
     * @return A corresponding filer
     */
    public static IMappingFilter createProfileFilter(final String profile) {
        return new IMappingFilter() {
            @Override
            public boolean copyDst2Src(IMappingHolder holder) {
                return profile.equals(holder.getProfile());
            }

            @Override
            public boolean copySrc2Dst(IMappingHolder holder) {
                return profile.equals(holder.getProfile());
            }
        };
    }
}
